package com.qzyh.esb.egsa.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qzyh.esb.egsa.bean.vo.RequirementContentVO;
import com.qzyh.esb.egsa.bean.vo.SystemInfoVO;
import com.qzyh.esb.egsa.entity.MainSystem;
import com.qzyh.esb.egsa.entity.Requirement;
import com.qzyh.esb.egsa.entity.RequirementContent;
import com.qzyh.esb.egsa.entity.SystemInfo;
import com.qzyh.esb.egsa.service.IMainSystemService;
import com.qzyh.esb.egsa.service.IRequirementContentService;
import com.qzyh.esb.egsa.service.IRequirementService;
import com.qzyh.esb.egsa.service.ISystemInfoService;
import com.qzyh.esb.egsa.utils.FillUtil;
import com.qzyh.esb.egsa.utils.PageResultObject;
import com.qzyh.esb.egsa.utils.ResultObject;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 系统信息表 前端控制器
 * </p>
 *
 * @author whq
 * @since 2023-11-02
 */
@RestController
@RequestMapping("/systemInfo")
public class SystemInfoController {
    private static final Logger logger = LoggerFactory.getLogger(RequirementController.class);

    @Autowired
    private IMainSystemService mainSystemService;

    @Autowired
    private ISystemInfoService systemInfoService;

    @GetMapping("/list")
    @ApiOperation(value = "查询系统信息列表")
    public PageResultObject<SystemInfoVO> list(@ApiParam(value = "页码") @RequestParam("pageNum") Long pageNum,
                                               @ApiParam(value = "每页条数") @RequestParam("pageSize") Long pageSize,
                                               @ApiParam(value = "系统名称") @RequestParam("name") String name,
                                               @ApiParam(value = "英文简称") @RequestParam("engName") String engName,
                                               @ApiParam(value = "consumerId") @RequestParam("consumerId") String consumerId) {
        IPage<SystemInfoVO> page = new Page<>(pageNum == null ? 0 : pageNum, pageSize == null ? 10 : pageSize);
        page = systemInfoService.getList(page, name, engName, consumerId);
        PageResultObject<SystemInfoVO> pageResultObject = PageResultObject.success();
        pageResultObject.setPageNum(page.getCurrent());
        pageResultObject.setPageSize(page.getSize());
        pageResultObject.setTotal(page.getTotal());
        pageResultObject.setData(page.getRecords());
        return pageResultObject;
    }

    @PostMapping("/save")
    @ApiOperation(value = "保存系统信息")
    public ResultObject save(@RequestBody SystemInfo systemInfo) {
        logger.info("请求参数：" + systemInfo.toString());
        systemInfo = (SystemInfo) FillUtil.fill(systemInfo);
        logger.info("填充后的信息：" + systemInfo.toString());
        boolean flag = systemInfoService.saveOrUpdate(systemInfo);
        if (flag) return ResultObject.success("保存成功");
        else return ResultObject.fail("保存失败");
    }

    @GetMapping("/delete")
    @ApiOperation(value = "根据id删除系统信息")
    public ResultObject deleteById(@ApiParam(value = "系统id") @RequestParam("id") String id) {
        logger.info("删除系统信息id：" + id);
        boolean flag = systemInfoService.removeById(id);
        if (flag) return ResultObject.success("删除成功");
        else return ResultObject.fail("删除失败");
    }

    @GetMapping("/getMainSysList")
    @ApiOperation(value = "根据关键字查询主系统信息")
    public ResultObject<List<MainSystem>> getMainSysList(@ApiParam(value = "查询关键字") @RequestParam("keywords") String keywords) {
        logger.info("查询关键字：" + keywords);
        return mainSystemService.getMainSysList(keywords);
    }

}
